package com.ruoyi.web.controller.gcgl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUpMoneyUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcJhksqd;
import com.ruoyi.gcgl.domain.GcJhksqditem;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcSupply;
import com.ruoyi.gcgl.domain.SjProcessConfig;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.IGcJhksqdService;
import com.ruoyi.gcgl.service.IGcJhksqditemService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcSendTipService;
import com.ruoyi.gcgl.service.IGcSupplyService;
import com.ruoyi.gcgl.service.ISjProcessConfigService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysRoleService;

/**
 * 借款申请Controller
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Controller
@RequestMapping("/gcgl/jhksqd")
public class GcJhksqdController extends BaseController
{
    private String prefix = "gcgl/jhksqd";

    @Autowired
    private IGcJhksqdService gcJhksqdService;
    @Autowired
    private IGcJhksqditemService gcJhksqditemService;
    @Autowired
    private IGcSupplyService gcSupplyService;
    @Autowired
    private IGcSendTipService gcSendTipService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Autowired
    private ISjProcessConfigService sjProcessConfigService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysRoleService roleService;
    
    final String processCode = "sjbaoxiao";
    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;
    
    @RequiresPermissions("gcgl:jhksqd:view")
    @GetMapping()
    public String jhksqd()
    {
        return prefix + "/jhksqd";
    }

    /**
     * 查询借款申请列表
     */
    @RequiresPermissions("gcgl:jhksqd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcJhksqd gcJhksqd)
    {
        startPage();
        gcJhksqd.setDeleteFlag(1);
      //角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	gcJhksqd.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		gcJhksqd.setOwnerId(getUserId());
            }
        }
        //----
        List<GcJhksqd> list = gcJhksqdService.selectGcJhksqdList(gcJhksqd);
        return getDataTable(list);
    }

    /**
     * 导出借款申请列表
     */
    @RequiresPermissions("gcgl:jhksqd:export")
    @Log(title = "借款申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcJhksqd gcJhksqd)
    {
    	gcJhksqd.setDeleteFlag(1);
    	if(getSysUser().isShow()){
            gcJhksqd.setCreateUser(getLoginName());
            }
        List<GcJhksqd> list = gcJhksqdService.selectGcJhksqdList(gcJhksqd);
        ExcelUtil<GcJhksqd> util = new ExcelUtil<GcJhksqd>(GcJhksqd.class);
        return util.exportExcel(list, "借款申请数据");
    }

    /**
     * 新增借款申请
     */
    @GetMapping("/add/{id}")
    public String add(ModelMap mmap,@PathVariable("id") Long id){
    	mmap.put("jkr", getLoginName());
    	mmap.put("jkrAt", DateUtils.getDate());
    	
    	GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplylis", gcSupplylis);
    	
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	mmap.put("owner", getLoginName());
    	mmap.put("ownerId", getUserId());
    	mmap.put("deptName", getSysUser().getDept().getDeptName());
    	mmap.put("deptId", getSysUser().getDept().getDeptId());
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/add";
    }

    /**
     * 新增保存借款申请
     */
    @RequiresPermissions("gcgl:jhksqd:add")
    @Log(title = "借款申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public synchronized AjaxResult addSave(GcJhksqd gcJhksqd)
    {
    	try {
    		SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
    		SimpleDateFormat sdm = new SimpleDateFormat("MM");
    		int year = Integer.parseInt(sdy.format(new Date()));
    		int month = Integer.parseInt(sdm.format(new Date()));
    		GcJhksqd newObj = new GcJhksqd();
    		newObj.setDeleteFlag(1);
    		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, month));
    		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, month));
    		List<GcJhksqd> lisCount = gcJhksqdService.selectGcJhksqdList(newObj);
    		
    		GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setName(gcJhksqd.getSkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcJhksqd.setSkdw(gcSupplylis.get(0).getName());
    			gcJhksqd.setSkzh(gcSupplylis.get(0).getSkzh());
            	gcJhksqd.setNssbh(gcSupplylis.get(0).getNssbh());
            	gcJhksqd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
    		
    		String no = String.format("%02d", lisCount.size()+1);
    		String l_month = String.format("%02d", month);
    		gcJhksqd.setOrderNo(year+"-"+l_month+"-"+no);
    		gcJhksqd.setDeleteFlag(1);
    		gcJhksqd.setCreateAt(new Date());
    		gcJhksqd.setUpdateAt(new Date());
    		gcJhksqd.setCreateUser(getLoginName());
    		gcJhksqd.setUpdateUser(getLoginName());
    		gcJhksqd.setStatus(0);
    		gcJhksqd.setRiq(new Date());
    		gcJhksqd.setJkr(getLoginName());
    		gcJhksqd.setJkrAt(new Date());
        	gcJhksqdService.insertGcJhksqd(gcJhksqd);
        	
        	Double hejexx = 0.0;
        	for(GcJhksqditem gcJhksqditem:gcJhksqd.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(gcJhksqditem.getJine().toString()));
        		gcJhksqditem.setDeleteFlag(1);
        		gcJhksqditem.setCreateAt(new Date());
        		gcJhksqditem.setUpdateAt(new Date());
        		gcJhksqditem.setCreateUser(getLoginName());
        		gcJhksqditem.setUpdateUser(getLoginName());
        		gcJhksqditem.setJhksqdId(gcJhksqd.getId());
        		gcJhksqditemService.insertGcJhksqditem(gcJhksqditem);
        	}
        	
        	DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
			gcJhksqd.setHejexx(mf.format(hejexx));
        	gcJhksqd.setJkrje(Double.valueOf(hejexx));
        	gcJhksqd.setHjje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcJhksqdService.updateGcJhksqd(gcJhksqd);
        	
        	//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 修改借款申请
     */
    @RequiresPermissions("gcgl:jhksqd:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(id);
        GcJhksqditem gcJhksqditem = new GcJhksqditem();
        gcJhksqditem.setJhksqdId(id);
        List<GcJhksqditem> items = gcJhksqditemService.selectGcJhksqditemList(gcJhksqditem);
        gcJhksqd.setItem(items);
        mmap.put("gcJhksqd", gcJhksqd);
        
        GcSupply gcSupply = new GcSupply();
    	gcSupply.setDeleteFlag(1);
    	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    	mmap.put("gcSupplylis", gcSupplylis);
    	
    	//下一级审核人
        List<SjProcessConfig> configlis = returnUser(getSysUser().getDept().getDeptId());
        mmap.put("approveid", configlis.get(0).getLevel1id());
        mmap.put("approvename", configlis.get(0).getLevel1name());
        
      //所属
        SysDept dept = new SysDept();
        dept.setDelFlag("1");
        dept.setParentId(100l);
        List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
        mmap.put("deptlis", deptlis);
        return prefix + "/edit";
    }

    /**
     * 修改保存借款申请
     */
    @RequiresPermissions("gcgl:jhksqd:edit")
    @Log(title = "借款申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcJhksqd gcJhksqd)
    {
    	try {
    		gcJhksqditemService.deleteGcJhksqditemById(gcJhksqd.getId());
    		
    		Double hejexx = 0.0;
        	//item
        	for(GcJhksqditem item : gcJhksqd.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(item.getJine().toString()));
        		item.setDeleteFlag(1);
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setJhksqdId(gcJhksqd.getId());
        		gcJhksqditemService.insertGcJhksqditem(item);
        	}
        	
        	GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setName(gcJhksqd.getSkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcJhksqd.setSkdw(gcSupplylis.get(0).getName());
    			gcJhksqd.setSkzh(gcSupplylis.get(0).getSkzh());
            	gcJhksqd.setNssbh(gcSupplylis.get(0).getNssbh());
            	gcJhksqd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
        	
        	gcJhksqd.setJkrje(Double.valueOf(hejexx));
        	DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
			gcJhksqd.setHejexx(mf.format(hejexx));
        	gcJhksqd.setHjje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcJhksqd.setUpdateAt(new Date());
        	gcJhksqd.setUpdateUser(getLoginName());
        	gcJhksqdService.updateGcJhksqd(gcJhksqd);
		} catch (Exception e) {
			e.printStackTrace();
			return toAjax(0);
		}
        return toAjax(1);
    }

    /**
     * 删除借款申请
     */
    @RequiresPermissions("gcgl:jhksqd:remove")
    @Log(title = "借款申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcJhksqdService.deleteGcJhksqdByIds(ids));
    }
    
    /////财务审批
    @RequiresPermissions("gcgl:jhksqd:cwpsview")
    @GetMapping("/cwps")
    public String cwps()
    {
        return prefix + "/jhksqdcwps";
    }
    
    @PostMapping("/cwpslist")
    @ResponseBody
    public TableDataInfo cwpslist(GcJhksqd gcJhksqd)
    {
        startPage();
        gcJhksqd.setDeleteFlag(1);
        gcJhksqd.setStatus(0);
        List<GcJhksqd> list = gcJhksqdService.selectGcJhksqdList(gcJhksqd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwyes")
    @ResponseBody
    public AjaxResult cwyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(1);
    		obj.setJkspr(getLoginName());
    		obj.setJksprAt(new Date());
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/cwno")
    @ResponseBody
    public AjaxResult cwno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(2);
    		obj.setJkspr(getLoginName());
    		obj.setJksprAt(new Date());
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	return toAjax(1);
    }
    
    /////总经理审批
    @RequiresPermissions("gcgl:jhksqd:zjlpsview")
    @GetMapping("/zjlps")
    public String zjlps()
    {
        return prefix + "/jhksqdzjlps";
    }
    
    @PostMapping("/zjlpslist")
    @ResponseBody
    public TableDataInfo zjlpslist(GcJhksqd gcJhksqd)
    {
        startPage();
        gcJhksqd.setDeleteFlag(1);
        gcJhksqd.setStatus(1);
        List<GcJhksqd> list = gcJhksqdService.selectGcJhksqdList(gcJhksqd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(3);
    		obj.setJkldsp(getLoginName());
    		obj.setJkldspAt(new Date());
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(4);
    		obj.setJkldsp(getLoginName());
    		obj.setJkldspAt(new Date());
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	return toAjax(1);
    }
    
    @Log(title = "确认还款", businessType = BusinessType.UPDATE)
    @PostMapping("/hk")
    @ResponseBody
    public AjaxResult hk(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setHkrAt(new Date());
    		obj.setHkr(getLoginName());
    		obj.setStatus(5);
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    //财务审批-还款
    @RequiresPermissions("gcgl:jhksqd:hkcwpsview")
    @GetMapping("/hkcwps")
    public String hkcwps()
    {
        return prefix + "/hksqdcwps";
    }
    
    @PostMapping("/hkcwpslist")
    @ResponseBody
    public TableDataInfo hkcwpslist(GcJhksqd gcJhksqd)
    {
        startPage();
        gcJhksqd.setDeleteFlag(1);
        gcJhksqd.setStatus(5);
        List<GcJhksqd> list = gcJhksqdService.selectGcJhksqdList(gcJhksqd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hkcwyes")
    @ResponseBody
    public AjaxResult hkcwyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(6);
    		obj.setHkspr(getLoginName());
    		obj.setHksprAt(new Date());
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hkcwno")
    @ResponseBody
    public AjaxResult hkcwno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(7);
    		obj.setHkspr(getLoginName());
    		obj.setHksprAt(new Date());
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	return toAjax(1);
    }
    
    /////总经理审批
    @RequiresPermissions("gcgl:jhksqd:hkzjlpsview")
    @GetMapping("/hkzjlps")
    public String hkzjlps()
    {
        return prefix + "/hksqdzjlps";
    }
    
    @PostMapping("/hkzjlpslist")
    @ResponseBody
    public TableDataInfo hkzjlpslist(GcJhksqd gcJhksqd)
    {
        startPage();
        gcJhksqd.setDeleteFlag(1);
        gcJhksqd.setStatus(6);
        List<GcJhksqd> list = gcJhksqdService.selectGcJhksqdList(gcJhksqd);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hkzjlyes")
    @ResponseBody
    public AjaxResult hkzjlyes(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(8);
    		obj.setHkjkldsp(getLoginName());
    		obj.setHkjkldspAt(new Date());
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/hkzjlno")
    @ResponseBody
    public AjaxResult hkzjlno(String ids, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcJhksqd obj = new GcJhksqd();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(9);
    		obj.setHkjkldsp(getLoginName());
    		obj.setHkjkldspAt(new Date());
    		gcJhksqdService.updateGcJhksqd(obj);
    		
    		GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(id[i]));
    		//发送消息通知
        	gcSendTipService.sendTip("jhk", "借款申请", gcJhksqd.getOrderNo(), 
        			gcJhksqd.getCreateUser(), DateUtils.dateTime(gcJhksqd.getCreateAt()), 
        			gcJhksqd.getStatus().toString(), null, gcJhksqd.getId().toString());
    	}
    	return toAjax(1);
    }
    
    //借款
    @GetMapping("/jksqdprint/{id}")
    public String jksqdprint(@PathVariable("id") Long id, ModelMap mmap){
    	GcJhksqd obj = gcJhksqdService.selectGcJhksqdById(id);
        
    	GcJhksqditem items = new GcJhksqditem();
        items.setJhksqdId(id);
        List<GcJhksqditem> itemlis= gcJhksqditemService.selectGcJhksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/jksqdprint";
    }
    
    //借款
    @GetMapping("/jksqdcwsp/{id}")
    public String jksqdcwsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcJhksqd obj = gcJhksqdService.selectGcJhksqdById(id);
        
    	GcJhksqditem items = new GcJhksqditem();
        items.setJhksqdId(id);
        List<GcJhksqditem> itemlis= gcJhksqditemService.selectGcJhksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/jksqdcwsp";
    }
    
    //借款
    @GetMapping("/jksqdzjlsp/{id}")
    public String jksqdzjlsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcJhksqd obj = gcJhksqdService.selectGcJhksqdById(id);
        
    	GcJhksqditem items = new GcJhksqditem();
        items.setJhksqdId(id);
        List<GcJhksqditem> itemlis= gcJhksqditemService.selectGcJhksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/jksqdzjlsp";
    }
    
    //还款
    @GetMapping("/hksqdcwsp/{id}")
    public String hksqdcwsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcJhksqd obj = gcJhksqdService.selectGcJhksqdById(id);
        
    	GcJhksqditem items = new GcJhksqditem();
        items.setJhksqdId(id);
        List<GcJhksqditem> itemlis= gcJhksqditemService.selectGcJhksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/hksqdcwsp";
    }
    
    //还款
    @GetMapping("/hksqdzjlsp/{id}")
    public String hksqdzjlsp(@PathVariable("id") Long id, ModelMap mmap){
    	GcJhksqd obj = gcJhksqdService.selectGcJhksqdById(id);
        
    	GcJhksqditem items = new GcJhksqditem();
        items.setJhksqdId(id);
        List<GcJhksqditem> itemlis= gcJhksqditemService.selectGcJhksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/hksqdzjlsp";
    }
    
    //还款
    @GetMapping("/hksqdprint/{id}")
    public String hksqdprint(@PathVariable("id") Long id, ModelMap mmap){
    	GcJhksqd obj = gcJhksqdService.selectGcJhksqdById(id);
        
    	GcJhksqditem items = new GcJhksqditem();
        items.setJhksqdId(id);
        List<GcJhksqditem> itemlis= gcJhksqditemService.selectGcJhksqditemList(items);
        if(itemlis!=null){
        	obj.setItem(itemlis);
        }
        
        mmap.put("obj", obj);
        return prefix + "/hksqdprint";
    }
    
    @GetMapping("/p4jhksqd/{id}")
    public String p4jhksqd(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailJhksqd";
    }

    /**
     * 查询借款申请列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(GcJhksqd gcJhksqd){
    	gcJhksqd.setDeleteFlag(1);
    	//角色数据权限 ==根据用户定义的数据权限查看项目数据
        List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
        int count = roleService.selectCountRoleDeptByRoleId(roleIds);
        if(count != 0){
        	//看部门
        	gcJhksqd.setRoleIds(roleIds);
        }else{
        	//看个人
        	if(getSysUser().isShowRoleIds(roleIds)){
        		gcJhksqd.setOwner(getLoginName());
            }
        }
        //----
        List<GcJhksqd> list = gcJhksqdService.selectGcJhksqdList(gcJhksqd);
        return getDataTable(list);
    }
    
  //---------------------审批方法
    //申请人部门PID
    private List<SjProcessConfig> returnUser(Long deptId){
    	SysDept dept = deptService.selectDeptById(deptId);
    	SjProcessConfig sjProcessConfig = new SjProcessConfig();
        sjProcessConfig.setDeleteFlag(1);
        sjProcessConfig.setCode(processCode);
        sjProcessConfig.setDeptId(dept.getParentId());
        List<SjProcessConfig> configlis = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig);
		
        if(configlis==null || configlis.size()==0){
        	SjProcessConfig sjProcessConfig1 = new SjProcessConfig();
        	sjProcessConfig1.setDeleteFlag(1);
        	sjProcessConfig1.setCode("morenliucheng");
            List<SjProcessConfig> configlis1 = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig1);
            return configlis1;
        }
        return configlis;
    }
    
    /**
     * 修改保存项目立项
     */
    @Log(title = "借还款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit4check")
    @ResponseBody
    public AjaxResult edit4check(GcJhksqd gcJhksqd){
    	try {
    		gcJhksqditemService.deleteGcJhksqditemById(gcJhksqd.getId());
    		
    		Double hejexx = 0.0;
        	//item
        	for(GcJhksqditem item : gcJhksqd.getItem()){
        		hejexx=DecimalCalculateUtil.add(hejexx, 
        				Double.parseDouble(item.getJine().toString()));
        		item.setDeleteFlag(1);
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setJhksqdId(gcJhksqd.getId());
        		gcJhksqditemService.insertGcJhksqditem(item);
        	}
        	
        	GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	gcSupply.setName(gcJhksqd.getSkdw());
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
    		if(gcSupplylis!=null && gcSupplylis.size()>0){
    			gcJhksqd.setSkdw(gcSupplylis.get(0).getName());
    			gcJhksqd.setSkzh(gcSupplylis.get(0).getSkzh());
            	gcJhksqd.setNssbh(gcSupplylis.get(0).getNssbh());
            	gcJhksqd.setKhyh(gcSupplylis.get(0).getKhyh());
    		}
        	
        	gcJhksqd.setJkrje(Double.valueOf(hejexx));
        	DecimalFormat mf = new DecimalFormat();
			mf.applyPattern("#0.00");
			gcJhksqd.setHejexx(mf.format(hejexx));
        	gcJhksqd.setHjje(ConvertUpMoneyUtil.toChinese(hejexx.toString()));
        	gcJhksqd.setUpdateAt(new Date());
        	gcJhksqd.setUpdateUser(getLoginName());
        	gcJhksqdService.updateGcJhksqd4Check(gcJhksqd);
		} catch (Exception e) {
			e.printStackTrace();
			return toAjax(0);
		}
        return toAjax(1);
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level1")
    public String level1(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcJhksqditem gcJhksqditem = new GcJhksqditem();
        	gcJhksqditem.setJhksqdId(gcJhksqd.getId());
            List<GcJhksqditem> items = gcJhksqditemService.selectGcJhksqditemList(gcJhksqditem);
            gcJhksqd.setItem(items);
            mmap.put("obj", gcJhksqd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcJhksqd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel2id());
            mmap.put("approvename", configlis.get(0).getLevel2name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level1check";
    }
    
    /**
     * level1
     * @return
     */
    @GetMapping("/level2")
    public String level2(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcJhksqditem gcJhksqditem = new GcJhksqditem();
        	gcJhksqditem.setJhksqdId(gcJhksqd.getId());
            List<GcJhksqditem> items = gcJhksqditemService.selectGcJhksqditemList(gcJhksqditem);
            gcJhksqd.setItem(items);
            mmap.put("obj", gcJhksqd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcJhksqd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel3id());
            mmap.put("approvename", configlis.get(0).getLevel3name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level2check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level3")
    public String level3(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcJhksqditem gcJhksqditem = new GcJhksqditem();
        	gcJhksqditem.setJhksqdId(gcJhksqd.getId());
            List<GcJhksqditem> items = gcJhksqditemService.selectGcJhksqditemList(gcJhksqditem);
            gcJhksqd.setItem(items);
            mmap.put("obj", gcJhksqd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcJhksqd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel4id());
            mmap.put("approvename", configlis.get(0).getLevel4name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level3check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level4")
    public String level4(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcJhksqditem gcJhksqditem = new GcJhksqditem();
        	gcJhksqditem.setJhksqdId(gcJhksqd.getId());
            List<GcJhksqditem> items = gcJhksqditemService.selectGcJhksqditemList(gcJhksqditem);
            gcJhksqd.setItem(items);
            mmap.put("obj", gcJhksqd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcJhksqd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel5id());
            mmap.put("approvename", configlis.get(0).getLevel5name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level4check";
    }
    /**
     * level1
     * @return
     */
    @GetMapping("/level5")
    public String level5(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcJhksqditem gcJhksqditem = new GcJhksqditem();
        	gcJhksqditem.setJhksqdId(gcJhksqd.getId());
            List<GcJhksqditem> items = gcJhksqditemService.selectGcJhksqditemList(gcJhksqditem);
            gcJhksqd.setItem(items);
            mmap.put("obj", gcJhksqd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            //下一级审核人
            List<SjProcessConfig> configlis = returnUser(Long.valueOf(gcJhksqd.getDeptId().toString()));
            mmap.put("approveid", configlis.get(0).getLevel6id());
            mmap.put("approvename", configlis.get(0).getLevel6name());
            
          //所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/level5check";
    }
    
    /**
     * 修改单据
     * @param taskid
     * @param mmap
     * @return
     */
    @GetMapping("/modify")
    public String modify(String taskid, ModelMap mmap){
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
        	GcJhksqd gcJhksqd = gcJhksqdService.selectGcJhksqdById(Long.parseLong(p.getBusinessKey().toString()));
        	GcJhksqditem gcJhksqditem = new GcJhksqditem();
        	gcJhksqditem.setJhksqdId(gcJhksqd.getId());
            List<GcJhksqditem> items = gcJhksqditemService.selectGcJhksqditemList(gcJhksqditem);
            gcJhksqd.setItem(items);
            mmap.put("obj", gcJhksqd);
        	
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("taskid", taskid);
            
            GcSupply gcSupply = new GcSupply();
        	gcSupply.setDeleteFlag(1);
        	List<GcSupply> gcSupplylis = gcSupplyService.selectGcSupplyList(gcSupply);
        	mmap.put("gcSupplylis", gcSupplylis);
        	
        	//所属
            SysDept dept = new SysDept();
            dept.setDelFlag("1");
            dept.setParentId(100l);
            List<SysDept> deptlis = deptService.selectDeptList4pId(dept);
            mmap.put("deptlis", deptlis);
        }
        
        return prefix + "/modifycheck";
    }
}